import Vue from 'vue'
import VueRouter from 'vue-router'

import Home from '../views/home/index.vue'
import Kind from '../views/kind/index.vue'
import Cart from '../views/cart/index.vue'
import User from '../views/user/index.vue'

import store from "../store/index"
// import Detail from '../views/detail/index.vue'

// ：**编程式导航在跳转到与当前地址一致的URL时会报错，但这个报错不影响功能 下面代码即可解决
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch((err) => err)
}
  
Vue.use(VueRouter)

const routes = [
  {
    path:'/',
    redirect:'/home'
  },
  {
    path: '/home',
    name: 'home',
    component: Home
  },
  {
    path: '/kind',
    name: 'kind',
    component: Kind
  },
  {
    path: '/cart',
    name: 'cart',
    component: Cart
  },
  {
    path: '/user',
    name: 'user',
    component: User,
    beforeEnter (to, from, next) { // 路由独享的导航守卫 -- 一般不建议使用
      if (store.state.loginState) {
        next()
      } else {
        
        next('/login')
      }
    }
  },
  {
    path:'/city',
    name:'city',
    component:() => import ('../views/city/index'),
    meta:{ //路由元信息
      hidden:true  // 不显示底部选项卡
    }
  },
  {
    path:'/detail/:proid',
    name:'detail',
    component: () => import(/* webpackChunkName: 'detail' */'../views/detail/index'),
    meta:{ //路由元信息
      hidden:true  // 不显示底部选项卡
    }
  },
  {
    path:'/register',
    redirect:'/register/step1'
  },
  {
    path:'/register',
    name:'register',
    component:() => import(/* webpackChunkName: 'user' */'../views/register/index.vue'),
    meta:{
      hidden:true
    },
    children: [
      {
        path:'step1',
        name: 'registerStep1',
        component: () => import(/* webpackChunkName: 'user' */'../views/register/step1.vue'),
        meta:{ hidden:true },
      },
      {
        path:'step2',
        name: 'registerStep2',
        component: () => import(/* webpackChunkName: 'user' */'../views/register/step2.vue'),
        meta:{ hidden:true },
      },
      {
        path:'step3',
        name: 'registerStep3',
        component: () => import(/* webpackChunkName: 'user' */'../views/register/step3.vue'),
        meta:{ hidden:true }

      }
    ]
  },
  {
    path:'/login',
    name:'login',
    component: () => import(/* webpackChunkName: 'user' */'../views/login/index.vue'),
    meta:{ hidden:true }
  },
  {
    path: '/order/:time',
    name: 'order',
    component: () => import('../views/order/index.vue'),
    meta:{ hidden:true },
  },
  {
    // 因为订单地址列表页面，点击 某一个地址时先修改订单地址，然后返回上一页
    // 修改订单的地址 需要知道 哪一个用户在哪一个时间内下的订单（一个时间内可以下很多个订单）
    path: '/orderaddresslist/:time',
    name: 'orderaddresslist',
    component: () => import('../views/order/list.vue'),
    meta:{ hidden:true },
  },
  {
    // 因为添加订单地址页面，点击 保存并使用该地址，然后返回确认订单页面
    // 修改订单的地址 需要知道 哪一个用户在哪一个时间内下的订单（一个时间内可以下很多个订单）
    path: '/orderaddressadd/:time',
    name: 'orderaddressadd',
    component: () => import('../views/order/add.vue'),
    meta:{ hidden:true },
  },
  {
    // 因为添加订单地址页面，点击 保存并使用该地址，然后返回确认订单页面
    // 修改订单的地址 需要知道 哪一个用户在哪一个时间内下的订单（一个时间内可以下很多个订单）
    path: '/address/update',
    name: 'update',
    component: () => import('../views/order/add2.vue'),
    meta:{ hidden:true },
  },
  {
    path: '/search',
    name: 'search',
    component: () => import('../views/search/index.vue'),
    meta:{ hidden:true }
  },
  {
    path: '/searchresult', // /searchresult?keyword=111
    name: 'searchresult',
    component: () => import('../views/search/result.vue'),
    meta:{ hidden:true }
  },
  {
    path:'*',
    component: () => import(/* webpackChunkName: 'error' */'../views/error/notFuond')
  }
]
  
const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export default router